package org.jeecg.modules.xl.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.xl.as.entity.XlOperateLog;
import org.jeecg.modules.xl.as.service.IXlOperateLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author deco
 * @create 2025-03-17
 */
@Component
public class XlLogUtil   {
    @Resource
    private IXlOperateLogService xlOperateLogService;
    @Async
    public   void insertLog( String billCode,
                                   String operateType,
                                   String operateDesc,
                                   Date operateTime,
                                   String preStatus,
                                   String currentStatus){
        XlOperateLog  log = new XlOperateLog(null,billCode,
                operateType,
                operateDesc,
                null,
                operateTime,
                preStatus,
                currentStatus);
        //System.out.println(Thread.currentThread().getName());
//        if ("[MyScheduler_Worker".equals(Thread.currentThread().getName())||"async-".equals(Thread.currentThread().getName())) {
//            // 如果当前是定时任务线程，就跳过 Shiro 操作
//            log.setOperateUser("Timer");
//        }else{
//            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//            log.setOperateUser(user.getUsername());
//        }
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            log.setOperateUser(user.getUsername());
        } catch (Exception e) {
            log.setOperateUser("系统");
        }

       // xlOperateLogService.save(log);
        return ;
    }

    public List<XlOperateLog> getOrderLog(String billCode){
        QueryWrapper<XlOperateLog> logQueryWrapper = new QueryWrapper<>();
        logQueryWrapper.eq("bill_code",billCode);
        return xlOperateLogService.list(logQueryWrapper);
    }
}
